<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { toNumber } from 'lodash-es'
import { GetData, PatchModifyType } from '@/api/modules/drawactprize'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()

const findFrom = ref({
  DrawId: '',
  Flag: '',
  ProductId: '',
  TicketId: '',
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20
let PageNum = toNumber(localStorage.getItem('currentPage')) || 1

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.DrawId && ({ DrawId: findFrom.value.DrawId })),
    ...(findFrom.value.Flag && ({ Flag: findFrom.value.Flag })),
    ...(findFrom.value.ProductId && ({ ProductId: findFrom.value.ProductId })),
    ...(findFrom.value.TicketId && ({ TicketId: findFrom.value.TicketId })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 修改状态
async function ModifyType(params: any) {
  await PatchModifyType(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      GetDataList(PageNum, PageSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}
// 添加
function AddData() {
  router.push({
    name: 'drawactprizeDetail',
  })
}
// 编辑
function EditData(id: number) {
  localStorage.setItem('currentPage', `${PageNum}`)
  router.push({
    name: 'drawactprizeDetail',
    query: {
      id,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return toNumber(localStorage.getItem('currentPage')) || 1
})

onMounted(() => {
  if (localStorage.getItem('currentPage') !== null) {
    GetDataList(toNumber(localStorage.getItem('currentPage')), 20)
    localStorage.removeItem('currentPage')
  }
  else {
    GetDataList()
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="抽奖活动ID">
                <el-input v-model="findFrom.DrawId" placeholder="请输入抽奖活动ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="活动状态">
                <el-select v-model="findFrom.Flag" placeholder="请选择" clearable>
                  <el-option label="启用" :value="1" />
                  <el-option label="停用" :value="-1" />
                </el-select>
              </el-form-item>
              <el-form-item label="实物商品Id">
                <el-input v-model="findFrom.ProductId" placeholder="请输入实物商品Id" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="券ID">
                <el-input v-model="findFrom.TicketId" placeholder="请输入券ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="prizeId" label="奖品ID" width="100" />
        <el-table-column header-align="center" prop="prizeName" label="奖品名称" width="300" />
        <el-table-column align="center" label="奖品图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.prizePic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="prizeType" label="奖品类型" width="100">
          <template #default="scope">
            <el-tag>
              {{ scope.row.prizeType === 1 ? '券'
                : scope.row.prizeType === 2 ? '实物'
                  : scope.row.prizeType === 3 ? '红包' : '未知' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="sort" label="顺序" width="100" />
        <el-table-column align="center" prop="productId" label="实物ID" width="100" />
        <el-table-column align="center" prop="ticketId" label="券ID" width="100" />
        <el-table-column align="center" prop="drawId" label="抽奖活动ID" width="150" />
        <el-table-column align="center" prop="change" label="中奖概率" width="100" />
        <el-table-column align="center" prop="prizeNums" label="奖品库存" width="100" />
        <el-table-column align="center" prop="usedNums" label="已消耗库存" width="100" />
        <!-- <el-table-column align="center" prop="endTime" label="活动结束时间" width="200" /> -->
        <el-table-column align="center" prop="sort" label="预留字段" width="100" />
        <el-table-column align="center" prop="addTime" label="添加时间" width="220" />
        <el-table-column align="center" label="是否启用" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.flag"
              :active-value="1"
              :inactive-value="-1"
              @click="ModifyType({ prizeId: scope.row.prizeId, flag: scope.row.flag })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.prizeId)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
